//   MVCWin32Extras (OpenGLView.cpp)
//
//   Copyright 2010 Eric Llewellyn
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#include "OpenGLView.h"

namespace MVCWin32Extras
{
	OpenGLView::OpenGLView()
	{
		hDC = NULL;
		hRC = NULL;
	}

	OpenGLView::~OpenGLView()
	{
		wglMakeCurrent(NULL, NULL);
		wglDeleteContext(hRC);
		ReleaseDC(GetHandle(), hDC);
	}

	void OpenGLView::Init()
	{
		PIXELFORMATDESCRIPTOR pfd;
		PIXELFORMATDESCRIPTOR *ppfd = &pfd;
		ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
		ppfd->nVersion = 1;
		ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
		ppfd->dwLayerMask = PFD_MAIN_PLANE;
		ppfd->iPixelType = PFD_TYPE_COLORINDEX;
		ppfd->cColorBits = 16;
		ppfd->cDepthBits = 16;
		ppfd->cAccumBits = 0;
		ppfd->cStencilBits = 0;

		hDC = GetDC(GetHandle());
		int pixelformat = ChoosePixelFormat(hDC, ppfd);
		SetPixelFormat(hDC, pixelformat, ppfd);

		hRC = wglCreateContext(hDC);
		wglMakeCurrent(hDC, hRC);
	}

	void OpenGLView::PreDraw()
	{
		glClearColor(0, 0, 0, 0);
		glClear(GL_COLOR_BUFFER_BIT);
	}

	void OpenGLView::Draw()
	{
	}

	void OpenGLView::PostDraw()
	{
		SwapBuffers(hDC);
	}

	HDC OpenGLView::GetHDC()
	{
		return hDC;
	}

	HGLRC OpenGLView::GetHGLRC()
	{
		return hRC;
	}
}
